package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppMingXiShouLiDao;
import com.vf.admin.client.dao.AppSetupDao;
import com.vf.admin.client.dao.AppUserDao;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.AppMingXiShouLiService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.AgeUtils;
import com.vf.admin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.guizuCacheMap;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiShouLiServiceImpl.java
 * @Description TODO
 * @createTime 2021年11月02日 15:40:00
 */
@Service
public class AppMingXiShouLiServiceImpl implements AppMingXiShouLiService {
    @Autowired
    AppMingXiShouLiDao appMingXiShouLiDao;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private AppSetupDao appSetupDao;

    @Override
    public void saveOrUpdate(AppMingXiShouLi appMingXiShouLi) {
        appMingXiShouLiDao.saveOrUpdate(appMingXiShouLi);
    }

    @Override
    public void updateGiftData(Integer giftid,String picture,String giftname) {
        appMingXiShouLiDao.updateGiftData(giftid,picture,giftname);
    }

    @Override
    public List<AppMingXiShouLi> findList(Integer userid, SplitPageBean splitPageBean) {
        return appMingXiShouLiDao.findList(userid, splitPageBean);
    }

    @Override
    public List<AppMingXiShouLi> findSongli(Integer userid, SplitPageBean splitPageBean) {
        return appMingXiShouLiDao.findSongli(userid,splitPageBean);
    }

    @Override
    public List<AppMingXiShouLi> selectList(Integer userId) {
        return appMingXiShouLiDao.selectList(userId);
    }

    @Override
    public List<AppMingXiShouLi> list(Map<String, Object> params, SplitPageBean splitPageBean) {
        return appMingXiShouLiDao.list(params, splitPageBean);
    }

    @Override
    public List<AppMingXiShouLi> listAfter(Map<String, Object> params) {
        return appMingXiShouLiDao.listAfter(params);
    }

    @Override
    public BigDecimal listSum(Map<String, Object> paraMap) {
        BigDecimal sum = BigDecimal.ZERO;
        List<AppMingXiShouLi> list = appMingXiShouLiDao.list(paraMap);
        for(AppMingXiShouLi b : list) {
            sum = sum.add(b.getFujianum());
        }
        return sum;
    }

    @Override
    public List<AppMingXiShouLi> leaderBoard(String time,String endtime) {
        return appMingXiShouLiDao.leaderBoard(time,endtime);
    }

    @Override
    public List<AppRankingDto> getRanking(Integer secondType, Map<String, Object> params) {
        List<AppMingXiShouLi> appMingXiShouLis = new ArrayList<>();
        ArrayList<AppRankingDto> appRankingDtos = new ArrayList<>();
        switch (secondType){
            case 1:
                appMingXiShouLis = appMingXiShouLiDao.getDayRanking(params);
                break;
            case 2:
                appMingXiShouLis = appMingXiShouLiDao.getWeekRanking(params);
                break;
            case 3:
                appMingXiShouLis = appMingXiShouLiDao.getMonthRanking(params);
                break;
            default:
                return null;
        }
        AppSetup setup = appSetupDao.get();
        if(appMingXiShouLis!=null && appMingXiShouLis.size()>0){
            appMingXiShouLis.forEach(e->{
                AppRankingDto appRankingDto = new AppRankingDto();
                AppUser appUser = appUserDao.get(e.getUserid());
                appRankingDto.setPic(appUser.getPic());
                appRankingDto.setNick(appUser.getNick());
                appRankingDto.setUserId(appUser.getId());
                BigDecimal getMoney = e.getTotalMoney().multiply(setup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                appRankingDto.setTotalMoney(getMoney);
                appRankingDto.setSex(appUser.getSex());
                appRankingDto.setMySign(appUser.getMysign());
                appRankingDto.setVoiceSignature(appUser.getVoiceSignature());
                appRankingDto.setYearOld(AgeUtils.getAgeByBirth(appUser.getBirthday()));
                appRankingDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());

                //判断用户是否是贵族
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    appRankingDto.setMedal(medal);
                } else {
                    appRankingDto.setMedal("");
                }
                appRankingDto.setNobleid(appUser.getNobleid());
                appRankingDto.setIsguizu(appUser.getIsguizu());
                if(appUser.getVipEndDate() != null) {
                    appRankingDto.setIsVip(TimeUtils.afterNow(appUser.getVipEndDate())? 1: 0);
                }

                appRankingDtos.add(appRankingDto);
            });
            return appRankingDtos;
        }
        return null;
    }
}
